package test.temp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;
import utils.SmallTool;
import utils.Utils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: gcr
 * @Date: 2023/2/11 16:17
 */
public class CheckPagePMUpdate {
    static String dbName2 = "xml_content_1_3qw";
    static String dbNameMy = "spider_pubmed_vip_extend";

    @SneakyThrows
    public static void main(String[] args) {
        // 用于检查错误页码 重新解析数据入库 sj2

        String errorPMID = "error_pmid";

        SmallTool.printMemory();
        Db use3307 = Db.use(DSFactory.get("local_articleadd"));
        List<Entity> allDbInfo = Utils.getAllDbInfo(errorPMID, use3307, 40, "pmid");
        List<Integer> pmid1 = allDbInfo.stream().map(o -> o.getInt("pmid")).collect(Collectors.toList());
        List<List<Integer>> split = ListUtil.split(pmid1, 100);


        split.forEach(list -> {
            String start = CollUtil.join(list, ",");
            String paramSql = "select * from " + dbName2 + " where  pmid in  (  " + start + " )  ";
            List<Entity> querys = null;
            try {
                querys = use3307.query(paramSql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            querys.parallelStream().forEach(entity -> {
                String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
                //解析html
                Document doc = Jsoup.parse(content);
                try {
                    // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                    doc.select("ReferenceList").remove();
                } catch (Exception e) {

                }
                List<Element> divList = new ArrayList<>();
                Elements pubmedArticles = doc.select("PubmedArticle");
                Elements pubmedBookArticles = doc.select("PubmedBookArticle");
                for (Element e : pubmedArticles) {
                    divList.add(e);
                }
                for (Element e : pubmedBookArticles) {
                    divList.add(e);
                }
                List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
                //直接入库
                for (Pubmed pubmed : pubmeds) {
                    try {
                        upData(pubmed, use3307);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
            // 删除本地表数据
            for (Integer integer : list) {
                Entity entity = new Entity();
                entity.setTableName(errorPMID);
                entity.set("pmid", integer);
                try {
                    use3307.del(entity);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        });
    }

    @SneakyThrows
    public static void upData(Pubmed pubmed, Db use) {
        Entity delE = new Entity();
        delE.setTableName(dbNameMy);
        delE.set("pmid", pubmed.getPmid());
        Entity entity1 = new Entity();
        entity1.set("pages", pubmed.getPages());
        use.update(entity1, delE);

    }
}
